package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteSearchHotKeyDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.form.search.HomeSiteSearchHotKeyForm;
import com.wsj.web.vo.homeSite.ComAdType;
import com.wsj.web.vo.homeSite.HomeSiteSearchHotKey;

/*****
 * 
 * @author fengbin
 *
 */
public class HomeSiteSearchHotKeyDaoImpl extends HibernateDaoSupport implements IHomeSiteSearchHotKeyDao
{

	/****
	 * 添加热门关键字
	 */
	public void addHomeSiteSearchHotKeyDao(HomeSiteSearchHotKey homeSiteSearchHotKey) throws DataAccessException 
	{
		this.getHibernateTemplate().save(homeSiteSearchHotKey);
		
	}
  
	/*****
	 * 通过id加载搜索分类对象
	 */
	public ComAdType loadComAdTypeByIdDao(Integer comAdTypeId) 
	{
		return  (ComAdType) this.getHibernateTemplate().get(ComAdType.class, comAdTypeId);
	}

	
	/******
	 * 通过关键字,搜索分类id 和添加/更新
	 */
	public List loadHomeSiteSearchHotKeyByComAdTypeIdAndKeywordsDao(Integer id,String keywords, Integer comAdTypeId, String addOrUpdate) 
	{
		String searchHql="";
		List li=null;
		if(addOrUpdate!=null  && addOrUpdate.equals("add"))
		{
			searchHql = " select HomeSiteSearchHotKey as h where h.keywords = ? and h.comAdType.id = ?";
			li = this.getHibernateTemplate().find(searchHql,new Object[]{keywords,comAdTypeId});
		}
		else if(addOrUpdate!=null  && addOrUpdate.equals("update"))
		{
			searchHql = " select HomeSiteSearchHotKey as h where h.keywords = ? and h.comAdType.id = ?  and h.id !=?";
			li = this.getHibernateTemplate().find(searchHql,new Object[]{keywords,comAdTypeId,id});
		}
		return li;
	}

	
	/******
	 * 通过id加载热门关键字
	 */
	public HomeSiteSearchHotKey loadHomeSiteSearchHotKeyByIdDao(Integer id) 
	{
		return (HomeSiteSearchHotKey)this.getHibernateTemplate().get(HomeSiteSearchHotKey.class, id.longValue());
	}

	/*****
	 * 通过条件加载结果
	 */
	public PageResultSet loadHomeSiteSearchHotKeyListDao(Integer page,Integer pageSize,HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm) 
	{
		 PageResultSet pageResultSet = new PageResultSet();

	        PerfectPageResultSet pageResult = new PerfectPageResultSet();

	        // 获取Session
	        Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

	        // 初始化SQL语句
	        StringBuffer stringBuffer = new StringBuffer(" from HomeSiteSearchHotKey as h where 1=1 ");
	        
	        // 设置每页显示得记录数和开始页
	        pageResult.setPage(page, pageSize);

	        // 参数
	        List<Comparable> list = new ArrayList<Comparable>();
	        
	        //通过搜索分类查询
	        if(homeSiteSearchHotKeyForm.getComAdTypeId() != null)
	        {
	            stringBuffer.append(" and h.comAdType.id = ? ");
	            list.add(homeSiteSearchHotKeyForm.getComAdTypeId());
	        }
	        
	        //关键字
	        if(homeSiteSearchHotKeyForm.getKeywords() != null && !homeSiteSearchHotKeyForm.getKeywords().equals(""))
	        {
	            stringBuffer.append(" and h.keywords like ? ");
	            list.add("%"+homeSiteSearchHotKeyForm.getKeywords()+"%");
	        }
	        
           // 是否审核,
	        if(homeSiteSearchHotKeyForm.getAudite() != null)
	        {
	            stringBuffer.append(" and h.audite =  ? ");
	            list.add(homeSiteSearchHotKeyForm.getAudite());
	        }
	        
	       //是否推荐
	        if(homeSiteSearchHotKeyForm.getIscommend() != null)
	        {
	            stringBuffer.append(" and h.iscommend =  ? ");
	            list.add(homeSiteSearchHotKeyForm.getIscommend());
	        }
	        
	       //是否删除
	        if(homeSiteSearchHotKeyForm.getIsDel() != null)
	        {
	            stringBuffer.append(" and h.isDel =  ? ");
	            list.add(homeSiteSearchHotKeyForm.getIsDel());
	        }
	        
	        stringBuffer.append(" order by h.id asc ");

	        // 把集合转换成数组
	        String[] strParams = Tool.toArray(list);

	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
	        
	        return pageResultSet;
	        
	        
	}

	
	/******
	 * 更新热门关键字
	 */
	public void updateHomeSiteSearchHotKeyDao(HomeSiteSearchHotKey homeSiteSearchHotKey) 
	{
		this.getHibernateTemplate().update(homeSiteSearchHotKey);
	}

	/******
	 * 获取该搜索分类下面的前几个审核的,未删除的的热门关键字.
	 * 通过最新时间进行排序
	 */
	public List loadTopHotKeyListDao(String typeName, Integer top) 
	{
	/*	DetachedCriteria detachedCriteria = DetachedCriteria.forClass(HomeSiteSearchHotKey.class);
		Criteria criteria =detachedCriteria.getExecutableCriteria(this.getSession());
		criteria.add(Expression.eq("comAdType.typeName", typeName));
		criteria.add(Expression.eq("audite", new Short("1")));
		criteria.add(Expression.eq("isDel", new Short("0")));
		detachedCriteria.addOrder(Order.desc("createTime"));
		criteria.setMaxResults(top);
		return criteria.list();*/
		
		String hql="select h  from  HomeSiteSearchHotKey as h where  h.comAdType.typeName = ? and h.audite = ? and h.isDel = ? order by createTime ";
		
		Query q=this.getSession().createQuery(hql);
		q.setParameter(0, typeName);
		q.setParameter(1,new Short("1"));
		q.setParameter(2, new Short("0"));
		
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
		
		
		
	}

}
